import {HTTP} from "../../utils/http.js";
import {UserModel} from "../../models/user.js";
import Dialog from '/../../miniprogram_npm/vant-weapp/dialog/dialog';
import { formatDate } from "../../utils/util.js";
import {Login} from "../../utils/login.js";
var _Login = new Login();
var _UserModel = new UserModel();
var http = new HTTP();
var _self;
Page({
  data: {
    userName:"",
    dateofEnrollment: '2013',
    dateofGraduation: '2017',
    degreeArr: ["专科", "本科", "硕士", "博士", "博士后", "其他"],
    degreeIndex:1,
    instituteArr: [],
    instituteIndex: 0,
    subjectArr: ["请选择"],
    subjectIndex: 0,
    classArr: ["请选择"],
    classIndex: 0,
    schoolColleageArr: [],
    tempSubjectList:[],
    departmentId:"",
    eduExpId:"",
    isEnd:false,
    ischecked:""
  },

  onLoad: function (options) {
    _self = this;
  },
  onShow:function(){
    _Login.login(function(){
      let user = wx.getStorageSync("usercache");
      console.log("user" + JSON.stringify(user));
      if (user.ischecked == "02"){
        wx.switchTab({
          url: '/pages/index/index'
        });
      }
      if (user.ischecked == "01" || user.ischecked == "03") {
        _self.setData({
          userName: user.userName,
          ischecked: user.ischecked
        })
        _self.getInitData();
      } else {
        _self.setData({
          ischecked: user.ischecked
        })

        _self.initCollegeInfo();
      }
    });
  },
  getInitData:function(){
    _UserModel.getEduForRealName(function(data){
        _self.setData({
            dateofEnrollment:data.startDate.substr(0,4),
            dateofGraduation: data.endDate.substr(0, 4),
            degreeIndex: _self.data.degreeArr.indexOf(data.eucationalBackground),
            eduExpId:data.id
        })
        _self.initCollegeInfo().then(function(){

          let college = data.college;
          let subject = data.specializedSubject;
          let className = data.className;
          console.log(college, subject, className);
          _self.setData({
            instituteIndex: _self.data.instituteArr.indexOf(college)
          })
          let update = {
            year: _self.data.dateofEnrollment,
            college: college
          }
          _self.getSchooInfo(update, "subjectArr").then(function(){
            _self.setData({
              subjectIndex: _self.data.subjectArr.indexOf(subject)
            })

            update.major=subject
            _self.getSchooInfo(update, "classArr").then(function(){
              _self.setData({
                classIndex: _self.data.classArr.indexOf(className)
              })
            });
          });
        });
    
   


    })
  }, 
  initCollegeInfo: function () {
    let date = this.data.dateofEnrollment;
    let updata = {
      year: date
    }
    return _self.getSchooInfo(updata, "instituteArr");
  },
  authAgain:function(){
    let userName = _self.data.userName;
    if (userName == '') {
      Dialog.alert({
        title: '提示',
        message: "请填写姓名。"
      });
      return;
    }

    if (!(this.data.instituteArr.length > 0 && this.data.instituteIndex> 0  && this.data.instituteArr[this.data.instituteIndex])) {
      Dialog.alert({
        title: '提示',
        message: "请选择学院,若无相关数据请联系管理员。"
      });
      return;
    }

    if (!(this.data.subjectArr.length > 0 && this.data.subjectIndex > 0 && this.data.subjectArr[this.data.subjectIndex])) {
      Dialog.alert({
        title: '提示',
        message: "请选择专业,若无相关数据请联系管理员。"
      });
      return;
    }
    if (!(this.data.classArr.length > 0 && this.data.classIndex > 0 && this.data.classArr[this.data.classIndex])) {
      Dialog.alert({
        title: '提示',
        message: "请选择班级,若无相关数据请联系管理员。"
      });
      return;
    }
    let params1 = {
      "id": _self.data.eduExpId,
      "schoolName": "内蒙古大学",
      "eucationalBackground": _self.data.degreeArr[_self.data.degreeIndex],
      "startDate": _self.data.dateofEnrollment + "-09-01 00:00:00",
      "endDate": _self.data.dateofGraduation + "-07-01 00:00:00",
      "college": _self.data.instituteArr[_self.data.instituteIndex],
      "specializedSubject": _self.data.subjectArr[_self.data.subjectIndex],
      "className": _self.data.classArr[_self.data.classIndex],
      "useForRealName": "1"
    }

    let params2 = {
      "userName": userName,
      "departmentId": _self.data.departmentId
    }
    _UserModel.auth(params2, function (data) {
      console.log(data)
      _UserModel.updateEdu(params1, function (res) {
        _self.setData({
          ischecked: "01"
        });
        
        Dialog.alert({
          title: '提示',
          message: "您修改了信息并申请重新认证,请稍后查询认证结果。"
        });
      })
    })
  },
  formSubmit:function(e){
      let userName = e.detail.value.userName;
    if (userName == '') {
      Dialog.alert({
        title: '提示',
        message: "请填写姓名。"
      });
      return;
    }

    if (!(this.data.instituteArr.length > 0 && this.data.instituteIndex && this.data.instituteArr[this.data.instituteIndex])) {
      Dialog.alert({
        title: '提示',
        message: "请选择学院,若无相关数据请联系管理员。"
      });
      return;
    }

    if (!(this.data.subjectArr.length > 0 && this.data.subjectIndex > 0 && this.data.subjectArr[this.data.subjectIndex])) {
      Dialog.alert({
        title: '提示',
        message: "请选择专业,若无相关数据请联系管理员。"
      });
      return;
    }
    if (!(this.data.classArr.length > 0 && this.data.classIndex > 0 && this.data.classArr[this.data.classIndex])) {
      Dialog.alert({
        title: '提示',
        message: "请选择班级,若无相关数据请联系管理员。"
      });
      return;
    }




    let params1 = {
      "schoolName": "内蒙古大学",
      "eucationalBackground": _self.data.degreeArr[_self.data.degreeIndex],
      "startDate": _self.data.dateofEnrollment + "-09-01 00:00:00",
      "endDate": _self.data.dateofEnrollment + "-07-01 00:00:00",
      "college": _self.data.instituteArr[_self.data.instituteIndex],
      "specializedSubject": _self.data.subjectArr[_self.data.subjectIndex],
      "className": _self.data.classArr[_self.data.classIndex],
      "useForRealName": "1"
    }
   
    let params2 = {
        "userName": userName,
        "departmentId": _self.data.departmentId
    }
    _UserModel.auth(params2,function(data){
      _UserModel.addEdu(params1,function(res){
          _self.setData({
            ischecked:"01"
          });

        Dialog.alert({
          title: '提示',
          message: "您已成功申请实名,请稍后查询认证结果。"
        });
          
      })
    })
  },
  userNameChange:function(e){
    this.setData({
      userName:e.detail.value
    })
  },
  enrollDateChange: function (e) {
    this.setData({
      dateofEnrollment: e.detail.value
    })
    //入学日期改变加载对应的学院
    let year = e.detail.value;
    _self.getSchooInfo({ "year": year }, "instituteArr");
  },
  graduateDateChange: function (e) {
    this.setData({
      dateofGraduation: e.detail.value
    })
  },
  classChange:function(e){
    _self.setData({
        classIndex: e.detail.value
    })
    let insititue = _self.data.instituteArr[_self.data.instituteIndex];
    let subject = _self.data.subjectArr[_self.data.subjectIndex];
    let className = _self.data.classArr[e.detail.value];
    var year = _self.data.dateofEnrollment;
    _self.data.tempSubjectList.forEach(function(item,index){
      if(item.year == year && item.college==insititue && item.major == subject && item.dicClass == className){
        _self.setData({
          departmentId: item.departmentId+""
        });
        console.log(_self.data.departmentId)
      }
    })   
  },
  collegeChange: function (e) {
    console.log(e.detail.value);
    _self.setData(
      {
        instituteIndex: e.detail.value
      }
    )
    let update = {
      year: this.data.dateofEnrollment,
      college: this.data.instituteArr[e.detail.value]
    }
    _self.getSchooInfo(update, "subjectArr");
  },
  subjectChange: function (e) {
    _self.setData({
        subjectIndex: e.detail.value
    })
    let update = {
      year: this.data.dateofEnrollment,
      college: this.data.instituteArr[this.data.instituteIndex],
      major: this.data.subjectArr[e.detail.value]
    }
    _self.getSchooInfo(update, "classArr");
  }, 
  getSchooInfo: function (params, target) {

    var promise = new Promise(function (resolve, reject) {
        http.request({
          url: "/api/getDicDepartment",
          data: params,
          success: function (data) {
            if (data.length == 0) {
              _self.setData({
                [target]: ["请选择"]
              });
              if (target == 'instituteArr') {
                _self.setData({
                  instituteIndex: 0
                })
              }
              if (target == 'subjectArr') {
                _self.setData({
                  subjectIndex: 0
                })
              }
              if (target == 'classArr') {
                _self.setData({
                  classIndex: 0
                })
              }

              return;
            }
            if (target == "classArr"){ 
              _self.setData({
                tempSubjectList:data,
                isEnd:true,
                departmentId:data[0].departmentId
              });
              console.log("默认的departmentId", _self.data.departmentId);
            }
            if (data.length > 0) {
              let dataArr = [];
              data.forEach(function (item, index) {
                if (target == 'instituteArr') {
                  dataArr.push(item.college);

                }
                if (target == 'subjectArr') {
                  dataArr.push(item.major)
                }
                if (target == 'classArr') {
                  dataArr.push(item.dicClass)
                }
              });
              dataArr.unshift("请选择");
              _self.setData({
                [target]: dataArr
              })
              if (target == 'subjectArr') {
                _self.setData({
                  subjectIndex: 0
                })
              }
              if (target == 'classArr') {
                _self.setData({
                  classIndex: 0
                })
              }
              // if (target == 'instituteArr') {
              //   let updata = {
              //     year: _self.data.dateofEnrollment,
              //     college: _self.data.instituteArr[_self.data.instituteIndex]
              //   }
              //   _self.getSchooInfo(updata, "subjectArr");
              // }
              // if (target == 'subjectArr') {
              //   let updata = {
              //     year: _self.data.dateofEnrollment,
              //     college: _self.data.instituteArr[_self.data.instituteIndex],
              //     major: _self.data.subjectArr[_self.data.subjectIndex]
              //   }
              //   _self.getSchooInfo(updata, "classArr");
              // }
            }
            resolve();
          }
        })
    });


    return promise;
  }
  ,
 　degreeChange:function(e){
   _self.setData({
     degreeIndex:e.detail.value
   })
  }
})